package com.winit.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateGenerator {

    /**
     * 去掉calendar中的时分秒（置为0）
     * @param calendar
     */
    private static final void resetCalendarForDate(Calendar calendar){
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    }

    /**
     * 获取当前日期，只精确到天
     * @return
     */
    public static final Date getNowDate(){
        return getDate(new Date());
    }

    /**
     * 获取日期，只精确到天
     * @return
     */
    public static final Date getDate(Date date){
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        resetCalendarForDate(calendar);
        return calendar.getTime();
    }



    /**
     * 获取下个星期五，精确到天
     * @return
     */
    public static final Date getNextFriday(){
        return getNextFriday(new Date());
    }
    /**
     * 获取下个星期五，精确到天
     * @return
     */
    public static final Date getNextFriday(Date date){
        Date tempDate = getNextFridayDateTime(date);
        Calendar calendar = Calendar.getInstance();
        if (tempDate != null) {
            calendar.setTime(tempDate);
        }
        resetCalendarForDate(calendar);
        return calendar.getTime();
    }

    /**
     * 获取下个星期五，精确到时分秒
     * @return
     */
    public static final Date getNextFridayDateTime(){
        return getNextFridayDateTime(new Date());
    }

    /**
     * 获取下个星期五，精确到时分秒
     * @return
     */
    public static final Date getNextFridayDateTime(Date date){
        Calendar calendar = Calendar.getInstance();
        if (null != date){
            calendar.setTime(date);
        }
        calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR) + 1);
        calendar.set(Calendar.DAY_OF_WEEK, 6);
        return calendar.getTime();
    }

    /**
     * 获取当前时间前三十天日期
     */
    public static final List<String> getFrontThirtyDays (){
        SimpleDateFormat sdf  = new SimpleDateFormat("MM-dd");
        String maxDateStr = sdf.format(new Date());
        String minDateStr = "";
        List<String> arr = new ArrayList<String>();
        Calendar calc =Calendar.getInstance();
        for(int i=0;i<30;i++){
            try {
                calc.setTime(sdf.parse(maxDateStr));
                calc.add(Calendar.DATE, -i);
                Date minDate = calc.getTime();
                minDateStr = sdf.format(minDate);
                arr.add(minDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return arr;
    }

    public static List<String> findDates(Date dBegin, Date dEnd){
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        List lDate = new ArrayList();
        lDate.add(sdf.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime()))
        {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(sdf.format(calBegin.getTime()));
        }
        return lDate;
    }

    /**
     * 时间戳转换字符串
     * @param date
     * @return
     */
    public static String getYYYY_MM_DD(Long date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sdate = sdf.format(new Date(date));
        return sdate;
    }

}
